package com.eorchis.module.getBaseData.ui.controller;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eorchis.constants.DeptConstants;
import com.eorchis.constants.RoleConstants;
import com.eorchis.core.basedao.base.IBaseDao;
import com.eorchis.core.page.commond.JSONObject;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.service.cache.BaseDataCacheUtil;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.news.domain.News;
import com.eorchis.module.security.cas.domain.Role;
import com.eorchis.module.systemparameter.service.ISystemParameterService;
import com.eorchis.module.user.domain.User;
import com.eorchis.module.util.CloneObject;
import com.eorchis.utils.utils.PropertyUtil;

@Controller("getBaseDataController")
@RequestMapping(value = GetBaseData.MODULE_PATH)
public class GetBaseData {
	
	@Resource(name = "com.eorchis.module.basedata.service.cache.BaseDataCacheUtil")
	private BaseDataCacheUtil baseDataCacheUtil;
	
	@Resource(name = "com.eorchis.module.systemparameter.service.impl.SystemParameterServiceImpl")
	private ISystemParameterService systemParameterService;
	
	@Autowired
	@Qualifier("com.eorchis.core.basedao.base.impl.BaseDao")
	private IBaseDao<Department> baseDao;
	
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/getBasedata";

	/**
	 * 根据数字字典类型编码获取对应的数字字典列表
	 * @param model
	 * @param request
	 * @param mcourseStatQueryCommond
	 * @param response
	 * @return
	 * @author Max
	 * @throws Exception 
	 * @date 2013-12-16
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@RequestMapping(value="/getBaseListByTypeCode")
	public @ResponseBody JSONObject getBaseDList(Model model,HttpServletRequest request,   
			HttpServletResponse response) throws Exception{
		String  typeCode = (String) request.getParameter("typeCode");
		JSONObject jsonResult = new JSONObject();
		if(typeCode != null){
			Map<String,List<BaseData>> baseDataMap =  (Map<String, List<BaseData>>) CloneObject.cloneObject(baseDataCacheUtil.getBaseDataList());
			List<BaseData>  baseDataList = baseDataMap.get(typeCode);
			
			//添加职级逻辑	byRongSL 2017-9-9
//			if(User.RANK_CODE_TYPE_ID.equals(typeCode)){
//				String dutyLevelCodes=null;
//				String nolevelCode=request.getParameter("nolevelCode");
//				String deptId=(String) request.getParameter("deptId");
//				String rankCode;
//				Role currentrole = (Role) request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
//				if(!PropertyUtil.objectNotEmpty(deptId)||deptId.equals("undefined")){
//					Department department = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
//					deptId=department.getID();
//				}
//				
//				Department dept=baseDao.findObject(Department.class, deptId);
//				if(dept.getTreepath().indexOf(DeptConstants.DEPT_JY_TREEPATH)!=-1)
//					dutyLevelCodes=systemParameterService.getSystemParameter("DEPT_JY_TREEPATH");
//				else if(dept.getTreepath().indexOf(DeptConstants.DEPT_GZW_TREEPATH)!=-1)
//					dutyLevelCodes=systemParameterService.getSystemParameter("DEPT_GZW_TREEPATH");
//				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_ONE_ADMIN))
//					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_ONE_ADMIN");
//				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_TWO_ADMIN))
//					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_TWO_OR_THREE_ADMIN");
//				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_THREE_ADMIN))
//					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_TWO_OR_THREE_ADMIN");
//				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_GJC_ADMIN))
//					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_GJC_ADMIN");
//					
//				if(null==dutyLevelCodes)
//					baseDataList=null;
//				else{
//					ListIterator<BaseData> ite=baseDataList.listIterator();
//					while(ite.hasNext()){
//						rankCode=ite.next().getDataID();
//						if(dutyLevelCodes.indexOf(rankCode)==-1)
//							ite.remove();
//						//新增人员时针对非干教处管理员去除局级显示
//						else if(PropertyUtil.objectNotEmpty(nolevelCode)&&!currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_GJC_ADMIN)&&(rankCode.equals("1380")||rankCode.equals("1381")))
//							ite.remove();
//					}
//				}
//			}
			
			jsonResult.setData(baseDataList);
			if(baseDataList != null){
				jsonResult.setTotalCount(baseDataList.size());
			}
			jsonResult.setSuccess(true);
		}else{
			jsonResult.setMsg("编码为空");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
	
	/**
	 * 根据数字字典类型编码获取对应的数字字典列表
	 * @param model
	 * @param request
	 * @param mcourseStatQueryCommond
	 * @param response
	 * @return
	 * @author Max
	 * @throws Exception 
	 * @date 2013-12-16
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@RequestMapping(value="/getBaseDListRankCode")
	public @ResponseBody JSONObject getBaseDListRankCode(Model model,HttpServletRequest request,   
			HttpServletResponse response) throws Exception{
		String  typeCode = (String) request.getParameter("typeCode");
		JSONObject jsonResult = new JSONObject();
		if(typeCode != null){
			Map<String,List<BaseData>> baseDataMap =  (Map<String, List<BaseData>>) CloneObject.cloneObject(baseDataCacheUtil.getBaseDataList());
			List<BaseData>  baseDataList = baseDataMap.get(typeCode);
			
			//添加职级逻辑	byRongSL 2017-9-9
			if(User.RANK_CODE_TYPE_ID.equals(typeCode)){
				String dutyLevelCodes=null;
				String deptId=(String) request.getParameter("deptId");
				String rankCode;
				Role currentrole = (Role) request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
				if(!PropertyUtil.objectNotEmpty(deptId)||deptId.equals("undefined")){
					Department department = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
					deptId=department.getID();
				}
				
				Department dept=baseDao.findObject(Department.class, deptId);
				if(dept.getTreepath().indexOf(DeptConstants.DEPT_JY_TREEPATH)!=-1)
					dutyLevelCodes=systemParameterService.getSystemParameter("DEPT_JY_TREEPATH");
				else if(dept.getTreepath().indexOf(DeptConstants.DEPT_GZW_TREEPATH)!=-1)
					dutyLevelCodes=systemParameterService.getSystemParameter("DEPT_GZW_TREEPATH");
				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_ONE_ADMIN))
					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_ONE_ADMIN");
				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_TWO_ADMIN))
					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_TWO_OR_THREE_ADMIN");
				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_THREE_ADMIN))
					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_TWO_OR_THREE_ADMIN");
				else if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_GJC_ADMIN))
					dutyLevelCodes=systemParameterService.getSystemParameter("ROLE_CODE_GJC_ADMIN");
				
				if(null==dutyLevelCodes)
					baseDataList=null;
				else{
					ListIterator<BaseData> ite=baseDataList.listIterator();
					while(ite.hasNext()){
						rankCode=ite.next().getDataID();
						if(dutyLevelCodes.indexOf(rankCode)==-1)
							ite.remove();
						//新增人员时针对非干教处管理员去除局级显示
						else if(rankCode.equals("1380")||rankCode.equals("1381"))
							ite.remove();
					}
				}
			}
			
			jsonResult.setData(baseDataList);
			if(baseDataList != null){
				jsonResult.setTotalCount(baseDataList.size());
			}
			jsonResult.setSuccess(true);
		}else{
			jsonResult.setMsg("编码为空");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
	/**
	 * 只为发送消息页面的消息类型用（去除培训班类型）
	 * @param model
	 * @param request
	 * @param mcourseStatQueryCommond
	 * @param response
	 * @return
	 * @author NingSS
	 * @throws Exception 
	 * @date 2016-6-30
	 */
	@RequestMapping(value="/getNewsType")
	public @ResponseBody JSONObject getNewsType(Model model,HttpServletRequest request,   
			HttpServletResponse response) throws Exception{
		//String  typeCode = (String) request.getParameter("typeCode");
		String typeCode=News.NEWS_TYPE;
		JSONObject jsonResult = new JSONObject();
		if(typeCode != null){
			Map<String,List<BaseData>> baseDataMap =  baseDataCacheUtil.getBaseDataList();
			List<BaseData>  baseDataList = baseDataMap.get(typeCode);
//			if(PropertyUtil.objectNotEmpty(baseDataList)){
//				for(int i=0;i<baseDataList.size();i++){
//					BaseData baseData=baseDataList.get(i);
//					if(baseData.getDataCode().equals(News.XXLX_PXB)){
//						baseDataList.remove(i);
//						break;
//					}
//				}
				jsonResult.setData(baseDataList);
//				if(baseDataList != null){
					jsonResult.setTotalCount(baseDataList.size());
//				}
//			}
			jsonResult.setSuccess(true);
		}else{
			jsonResult.setMsg("编码为空");
			jsonResult.setSuccess(false);
		}
		return jsonResult;
	}
}
